package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.wx.WxFootPrintService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: chenjian
 * @Date: 2024/5/19 19:12
 */
@RestController
@RequestMapping("/wx/footprint")
public class WxFootPrintController {
    @Autowired
    SecurityManager securityManager;
    @Autowired
    WxFootPrintService wxFootPrintService;

    @GetMapping("/list")
    public Object list(int page,int limit){
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        List<MarketGoods> list = wxFootPrintService.list(page, limit, user.getId());
        return ResponseUtil.okList(list);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody Map<String,String> map){
        String GoodsSn = map.get("id");

        try {
            Boolean delete = wxFootPrintService.delete(GoodsSn);
            if(delete){
                return ResponseUtil.ok();
            }else {
                return ResponseUtil.badArgument();
            }
        }catch (Exception e){
            return ResponseUtil.badArgument();
        }
    }
}
